/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lxj.easyadapter.sample;

import com.lxj.easyadapter.EasyAdapter;
import com.lxj.easyadapter.ItemDelegate;
import com.lxj.easyadapter.MultiItemTypeAdapter;
import com.lxj.easyadapter.ViewHolder;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayoutManager;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class AuxiliaryMainAbility extends Ability {

    @NotNull
    private final List<User> userList = new ArrayList<>();
    private EasyAdapter<User> adapter;
    private MultiItemTypeAdapter<User> multiItemTypeAdapter;

    private ListContainer listContainer;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        listContainer = (ListContainer)this.findComponentById(ResourceTable.Id_recyclerView);
        DirectionalLayoutManager layoutManager = new DirectionalLayoutManager();
        listContainer.setLayoutManager(layoutManager);
        //prepare data
        for (int i = 0; i <= 6; ++i) {
            userList.add(new User("本杰明 - " + i, i * 2, i));
        }
        // testHeader();
        testMultiItem();
    }

    private void testHeader() {
        adapter = new EasyAdapter<User>(userList, ResourceTable.Layout_item) {
            protected void bind(@NotNull ViewHolder holder, @NotNull User user, int position) {
                holder.setText(ResourceTable.Id_tv_name, "name: " + user.getName());
                holder.setText(ResourceTable.Id_tv_age, "age: " + user.getAge());
            }
        };
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.SimpleOnItemClickListener() {
            public void onItemClick(@NotNull Component view, @NotNull ViewHolder holder, int position) {
                super.onItemClick(view, holder, position);
                ToastDialog toast = new ToastDialog(AuxiliaryMainAbility.this);
                toast.setText("position - " + position);
                toast.show();
                userList.remove(position);
                adapter.notifyDataSetItemRemoved(position + adapter.getHeadersCount());
            }
        });
        adapter.addHeaderView(createView("Header - 1，点我在头部添加一条数据", false));
        adapter.addHeaderView(createView("Header - 2，点我在头部添加一条数据", false));
        adapter.addFootView(createView("Footer - 1，点我在末尾添加一条数据", true));
        adapter.addFootView(createView("Footer - 2，点我在末尾添加一条数据", true));

        listContainer.setItemProvider(adapter);
    }

    private Text createView(String text, boolean isFooter) {
        final Text textView = new Text(this);
        textView.setLayoutConfig(new ComponentContainer.LayoutConfig(
                ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT));
        textView.setPadding(80, 80, 80, 80);
        textView.setTextSize(16, Text.TextSizeType.FP);
        textView.setText(text);
        textView.setClickedListener(it -> {
            ToastDialog toast = new ToastDialog(AuxiliaryMainAbility.this);
            toast.setText(textView.getText());
            toast.show();
            User user = new User();
            user.setName("随机添加的名字 - " + System.currentTimeMillis());
            user.setAge((int)System.currentTimeMillis());
            if (isFooter) {
                userList.add(user);
            } else {
                userList.add(0, user);
            }
            if (multiItemTypeAdapter != null) {
                multiItemTypeAdapter.notifyDataChanged();
            }
            if (adapter != null) {
                adapter.notifyDataChanged();
            }
        });
        return textView;
    }

    public void testMultiItem() {
        multiItemTypeAdapter = new MultiItemTypeAdapter<>(userList);
        multiItemTypeAdapter.addItemDelegate(new OneDelegate());
        multiItemTypeAdapter.addItemDelegate(new TwoDelegate());
        multiItemTypeAdapter.addItemDelegate(new ThreeDelegate());
        multiItemTypeAdapter.addHeaderView(createView("Multi Header view1111", false));
        multiItemTypeAdapter.addHeaderView(createView("Multi Header view22222", false));
        multiItemTypeAdapter.addHeaderView(createView("Multi Header view33333", false));
        multiItemTypeAdapter.addFootView(createView("Multi Footer view", false));
        multiItemTypeAdapter.addFootView(createView("My Footer view 1", false));
        multiItemTypeAdapter.addFootView(createView("My Footer view 2", false));
        multiItemTypeAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {

            @Override
            public void onItemClick(@NotNull Component view, @NotNull ViewHolder holder, int position) {
                ToastDialog toast = new ToastDialog(AuxiliaryMainAbility.this);
                toast.setText("position: " + position);
                toast.show();
            }

            @Override
            public boolean onItemLongClick(@NotNull Component view, @NotNull ViewHolder holder, int position) {
                return false;
            }
        });
        listContainer.setItemProvider(multiItemTypeAdapter);
    }

    public class OneDelegate implements ItemDelegate<User> {

        @Override
        public int getLayoutId() {
            return ResourceTable.Layout_simple_list_item;
        }

        @Override
        public boolean isThisType(@NotNull User item, int position) {
            return position % 3 == 1;
        }

        @Override
        public void bind(@NotNull ViewHolder holder, @NotNull User user, int position) {
            holder.setText(ResourceTable.Id_text1, "name: " + user.getName() + " - " + position);
        }
    }

    public class TwoDelegate implements ItemDelegate<User> {

        @Override
        public int getLayoutId() {
            return ResourceTable.Layout_simple_list_item;
        }

        @Override
        public boolean isThisType(@NotNull User item, int position) {
            return position % 3 == 2;
        }

        @Override
        public void bind(@NotNull ViewHolder holder, @NotNull User user, int position) {
            holder.setText(ResourceTable.Id_text1, "age: " + user.getAge());
            ShapeElement element = new ShapeElement();
            element.setRgbColor(RgbColor.fromArgbInt(Color.RED.getValue()));
            holder.getView(ResourceTable.Id_text1).setBackground(element);
        }
    }

    public class ThreeDelegate implements ItemDelegate<User> {

        @Override
        public int getLayoutId() {
            return ResourceTable.Layout_image_list_item;
        }

        @Override
        public boolean isThisType(@NotNull User item, int position) {
            return position % 3 == 0;
        }

        @Override
        public void bind(@NotNull ViewHolder holder, @NotNull User user, int position) {
            holder.setImageResource(ResourceTable.Id_image1, ResourceTable.Media_icon);
        }
    }
}
